<div id="content">
    <div class="post">
        <h2 class="title">Kelola Plasa</h2>
        <div style="clear: both;">&nbsp;</div>
    </div>
    <div class="block-table">
        <table id="table-id" style="width:100%">
            <thead>
                <tr>
                    <th>Nama Plasa</th>
                    <th>Alamat</th>
                    <th>Kontak PIC</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($plasas as $plasa => $data) {
                    ?>
                    <tr>
                        <td>Plasa <?= $data['name']; ?></td>
                        <td><?= $data['address']; ?></td>
                        <td><?= $data['pic_contact']; ?></td>
                        <td><a class="update-plasa anchor-button" plasa_id="<?= $data['plasa_id']; ?>">Ubah</a> 
                            | <a href="javascript:confirm_delete('<?= base_url(); ?>admin/plasa/delete',<?= $data['plasa_id']; ?>)">Hapus</a>
                        </td>
                    </tr>
                    <?php
                }
                ?>
            </tbody>
        </table>

        <button id="add-plasa" class="button-add">Tambah Plasa</button>
        <div class="clearboth"></div>
    </div>
</div> 

<div class="dialog-container">
    <div id="insert-dialog" class="dialog">
        <fieldset>
            <legend>Tambah Plasa</legend>
            <div class="input-block"><label>Nama Plasa</label><input class="input-right" type="text" id="name_add"></div>
            <div class="input-block"><label>Alamat Plasa</label><input class="input-right" type="text" id="address_add"></div>
            <div class="input-block"><label>Kontak PIC</label><input class="input-right" type="text" id="pic_contact_add"></div>

            <br>
            <div class="center submit-container">
                <button id="submit-add">Simpan</button>
                <button class="cancel">Batal</button>
            </div>
        </fieldset>
    </div>

    <div id="update-dialog" class="dialog">
        <fieldset>
            <legend>Ubah informasi untuk : <span id="plasa_title_update"></span></legend>
            <input class="input-block" type="hidden" id="plasa_id_update">
            <div class="input-block"><label>Nama Plasa</label><input class="input-right" type="text" id="name_update"></div>
            <div class="input-block"><label>Alamat</label><input class="input-right" type="text" id="address_update"></div>
            <div class="input-block"><label>Kontak PIC</label><input class="input-right" type="text" id="pic_contact_update"></div>

            <div class="center submit-container">
                <button id="submit-update">Simpan</button>
                <button class="cancel">Batal</button>
            </div>
        </fieldset>
    </div>
</div>

<script type="text/javascript">
    var base_url = "<?= base_url(); ?>";
	
    $(function(){
        init_click();
        $('#table-id').dataTable({
            'aoColumns': [
                {'sWidth': '300px'},
                {'sWidth': '400px'},
                {'sWidth': '300px'},
                {'sWidth': '300px'}
            ]
        });
    });
	
    function init_click(){
        //add
        $("#add-plasa").click(function(){
            close_dialog(); 
            //$('#insert-dialog').dialog('open');
            $('#insert-dialog').show();
        });
        $("#submit-add").click(function(){
            var postdata = {
                name: $("#name_add").val(),
                address: $("#address_add").val(),
                pic_contact: $("#pic_contact_add").val()
            };
			
            $.post(base_url + 'admin/plasa/add', postdata, function(ret) {
                //alert(ret);
                window.location.reload();
            });
        });
        //update
        $(".update-plasa").click(function(){
            close_dialog();
		
            var plasa_id = $(this).attr('plasa_id');
			
                        //alert(base_url + 'admin/plasa/getplasa/' + plasa_id);
            $.get(base_url + 'admin/plasa/getplasa/' + plasa_id,
            function(data) {
                //alert("ada data"+data.name+data.plasa_id+data.address+data.pic_contact_id);
                $("#plasa_title_update").text(data.name),
                $("#plasa_id_update").val(data.plasa_id),
                $("#name_update").val(data.name),
                $("#address_update").val(data.address),
                $("#pic_contact_update").val(data.pic_contact);
				   
            }, "JSON");
			   
            $('#update-dialog').show();
        });
        $("#submit-update").click(function(){
			
            var postdata = {
                plasa_id : $("#plasa_id_update").val(),
                name: $("#name_update").val(),
                address: $("#address_update").val(),
                pic_contact: $("#pic_contact_update").val()
            };
			
            $.post(base_url + 'admin/plasa/update', postdata, function(ret) {
                //alert(ret);
                window.location.reload();
            });
        });
	
        //cancel
        $(".cancel").click(function(){
            close_dialog();
        });
    }
	
    //cancel
    function close_dialog(){
        $('#plasa_add').val("");
        $('#address_add').val("");
        $('#pic_contact_add').val("");
        $('#plasa_title_update').val("");
        $('#plasa_id_update').val("");
        $('#name_update').val("");
        $('#address_update').val("");
        $('#pic_contact_update').val("");
        
        $('.dialog').hide();
    }
    //delete
    function confirm_delete(url, plasa_id) {
        if (confirm("Apakah Anda yaking ingin menghapus?")) {
            $.post(url, {plasa_id: plasa_id}, function(ret) {
                //alert(ret);
                window.location.reload();
            });
        }else{
            //document.location = '';
        }
    }
</script>
